const utilApi = require('../../utils/util.js')
const alertApi = require('../../utils/alert.js')

const app = getApp()
var that

Page({
  data: { // 页面的初始数据
    ec: {
      onInit: initChart
    },
    actionSheet:{
      show: false,
      groups:[
        { text: '187-3421-8872', value: 18734218872 },
      ],
      title: '拨打工作人员的电话'
    },
    userInfo:{
      avatarUrl: "/images/my/unLogin.png",
      nickName: "未登录"
    },
    deviceID: "未上传",
    isLogin: false,
    isLoading: false,
  },
  
  onLoad: async function (options) { // 生命周期函数--监听页面加载
    that = this
    await app.isAuthorized()
    if(app.globalData.userInfo  != undefined ){
      this.setData({
        userInfo : app.globalData.userInfo,
        isLogin: true
      })
    }
  },
  
  rotate: async function() { // 用户更新个人信息
		var animation = wx.createAnimation({
			duration: 1000,
			timingFunction: "ease"
		});
		this.animation = animation
		animation.rotate(760).step()
		this.setData({
			animationData: animation.export()
    });

    var userInfo = await utilApi.getUserInfo()
    if (userInfo != undefined){
      app.globalData.userInfo = userInfo
      alertApi.tipsSuccess(that, "更新成功", 3000)
      this.setData({
        userInfo: userInfo,
        isLogin: true
      })
    } 
    else alertApi.tipsError(that, "更新失败", 3000)
    
		setTimeout(() => {
			animation.rotate(0).step()
			this.setData({
				animationData: animation.export()
			})
		}, 1200)
  },
  
  scanDeviceID: function () {
    that.setData({ isLoading: true })
    wx.scanCode({
      onlyFromCamera: true,
    }).then(res=>{
      var ID = app.verifyID(res.result)
      if(ID){
        app.globalData.primID =  ID
        app.globalData.affiID = []
        that.setData({ deviceID: ID })
        alertApi.tipsSuccess(that, "设备ID上传成功", 3000)
      }  
      else alertApi.tipsError(that, "设备ID未识别", 3000)
    },function (fail) { 
      alertApi.tipsInfo(that, "未完成扫码", 3000) 
    }).finally( ()=>{ that.setData({ isLoading: false }) } )
  },

  actionSheetClick: function () {
    that.data.actionSheet.show = true
    this.setData({
      actionSheet: that.data.actionSheet
    })
  },


  phone(e) {
    that.data.actionSheet.show = false
    this.setData({
      actionSheet: that.data.actionSheet
    })
    wx.makePhoneCall({
      phoneNumber: String(e.detail.value)
    })
  },
  navigateTo: function (e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
})

var echarts = require("../../ec-canvas/echarts.js");
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: '场馆在线人数统计',
      left: 'center'
    },
    color: ["#37A2DA"],
    grid: {
      containLabel: true,
      x: 10,
      y: 25,
      x2: 30,
      y2: 20
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['14:52', '15:02', '15:12', '15:22', '15:32', '15:42', '15:52', '16:02', '16:12', '16:22', '16:32', '16:42', '16:52'],
      // show: false
    },
    yAxis: {
      x: 'center',
      min: 140,
      max: 170,
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: 'Person',
      type: 'line',
      smooth: true,
      data: [142, 156, 162, 160, 150, 145, 153, 152, 163, 153, 153, 153, 155]
    }],
  };

  chart.setOption(option);
  return chart;
}